package com.etc.service.impl;

import com.etc.dao.UserMapper;
import com.etc.entity.User;
import com.etc.entity.UserExample;
import com.etc.entity.UserKey;
import com.etc.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author kalista
 * @Description
 * @Date 2021/1/4  17:00
 **/
@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findAll() {
        try {
        List<User> users = userMapper.selectByExample(null);
        return users;
    }catch (Exception e){
        e.printStackTrace();
    }
       return null;
}

    @Override
    public User getById(int uId) {
        try {

            UserKey userKey = new UserKey();
            userKey.setuId(uId);
            return userMapper.selectByPrimaryKey(userKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<User> findUserCupByName(String userName) {
        try {
            UserExample userExample = new UserExample();
            UserExample.Criteria criteria = userExample.createCriteria();
            criteria.andUserNameEqualTo(userName);
            return userMapper.selectByExample(userExample);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    @Override
    public Integer add(User user) {
        try {
            return userMapper.insert(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Integer deleteById(Integer uId) {
        try {
            UserKey userKey = new UserKey();
            userKey.setuId(uId);
            return userMapper.deleteByPrimaryKey(userKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Integer update(User user) {
        try {
            return userMapper.updateByPrimaryKeySelective(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
